<?php

namespace addons\xykeep\listener;
use app\api\model\xykeep\Config;
use \app\common\model\TeacherAudit;
use app\common\model\User as UserModel;
use \app\common\model\Teacher as TeacherModel;
use \app\common\model\Grade;
use think\Config as ConfigModel;
use think\Db;
use think\Exception;

class Teacher
{

    //教师申请通过
    public function teacherToPass($row)
    {
        $teacherModel = new TeacherModel();
        $teacher = $teacherModel::where([
            'user_id' => $row['user_id'],
        ])->find();

        $userModel = new UserModel();
        $user = $userModel::where('id',$row['user_id'])->find();


        //初次上传资料的时候，不上传头像时选择默认头像
        $avatar = '';

        if($row['avatar']){
            $avatar = $row['avatar'];
        }

        //新建教师的时候，不管怎么样都给他个默认头像
        if(!$teacher){
            if(!$avatar && $user['avatar']){
                $avatar = $user['avatar'];
            }
            if(!$avatar){
                $avatar = Config::getValueByName('xykeep')['useravatar'];
            }
        }
        if($avatar){
            $user->avatar = $avatar;
        }

        $user->nickname = $row['nickname'];
        $user->username = $row['nickname'];

        $user->save();

        $isUpdate = false;
        if($teacher){
            $isUpdate = true;
        }

        $settled_all = $row['settled_type'] . ',' . $row['settled_type_free'];
        $settled_all = explode(',', $settled_all);
        $settled_all = array_filter($settled_all);
        $settled_all = implode(',', $settled_all);

        if(isset($row['holiday_area_text'])){
            $holiday_area_arr = $row['holiday_area_text'];
        }else{
            $holiday_area_arr = explode(',',$row['holiday_area']);
        }

        if(isset($row['unholiday_area_text'])){
            $unholiday_area_arr = $row['unholiday_area_text'];
        }else{
            $unholiday_area_arr = explode(',',$row['unholiday_area']);
        }

        if(isset($row['give_lessons_text'])){
            $give_lessons_arr = $row['give_lessons_text'];
        }else{
            $give_lessons_arr = json_decode($row['give_lessons'],true);
        }

        $updateData = [
            'user_id' => $row['user_id'],
            'nickname' => $row['nickname'],
            'realname' => $row['realname'],
            'give_time' => $row['give_time'],
            'holiday_area_lat' => $holiday_area_arr[2],
            'holiday_area_lng' => $holiday_area_arr[3],
            'unholiday_area_lat' => $unholiday_area_arr[2],
            'unholiday_area_lng' => $unholiday_area_arr[3],
            'grade_id' => $give_lessons_arr[0]['grade'],
            'subject_id' => $give_lessons_arr[0]['subject'],
            'age_range_id' => Grade::where([
                'id' => $give_lessons_arr[0]['grade'],
            ])->value('age_range_id'),
            'audit_id' => $row['id'],
            'tags' => $row['tags'],
            'settled_type' => $settled_all,
            'sex' => $row['sex'],
            'deletetime' => null
        ];

        if($avatar){
            $updateData['avatar'] = $avatar;
        }

        //初始
        $star = 0;
        if(!$isUpdate){
            $star = ConfigModel::get('site.init_star');
        }

        //完善视频资料
        if($row['person_video']){
            $is_movie_fill = Db::name('star_log')->where([
                'user_id' => $row['user_id'],
                'type' => '完善视频资料'
            ])->find();
            if(!$is_movie_fill){
                $star += ConfigModel::get('site.move_fill');
                Db::name('star_log')->insert([
                    'user_id' => $row['user_id'],
                    'type' => '完善视频资料',
                    'before' => $teacher['star'],
                    'after' => $star,
                    'createtime' => time(),
                ]);
            }
        }
        if($star > 0){
            $updateData['star'] = $star;
        }

        //推荐为优选名师
        if(isset($row['recommend']) && $row['recommend']){
            $updateData['recommend'] = $row['recommend'];
        }

        $teacherModel->isUpdate($isUpdate)->save($updateData);
    }

    //星级更变
    public function teacherStarChange($param)
    {
        $teacher = TeacherModel::where([
            'user_id' => $param['user_id'],
        ])->find();
        if($teacher){
            $star = $teacher['star'] + $param['star'];
            if($star > 5){
                $star = 5;
            }else if($star <1){
                $star = 1;
            }
            $teacher->star = $star;

            if($star >= 4){
                //应该在教师列表
                if($teacher['recommend']==0){
                    $teacher->recommend = 1;
                }
            }else{
                //应该退出教师列表
                if($teacher['recommend']==1){
                    $teacher->recommend = 0;
                }
            }

            $teacher->save();

            Db::name('star_log')->insert([
                'user_id' => $param['user_id'],
                'type' => $param['type'],
                'before' => $teacher['star'],
                'after' => $star,
                'createtime' => time(),
            ]);

        }
    }

}